﻿package DevilCat{
//	import flash.display.MovieClip;
//	import flash.text.TextField;
//	import flash.events.MouseEvent;
	import DevilCat.GameData;
	import DevilCat.PokemonMove;
	import DevilCat.ItemObject;
	public class PokemonObject {
		public var pid:Number;
		public var upid:Number;
		public var pmod:Number;
		public var Name:String;
		public var Type1:String;
		public var Type2:String;
		public var Ability:String;
		public var HP:Number;
		public var OldHP:Number;
		public var Attack:Number;
		public var Defense:Number;
		public var SpAtk:Number;
		public var SpDef:Number;
		public var Speed:Number;
		public var Total:Number;
		public var Accuracy:Number;
		public var Evasive:Number;
		public var MaxHP:Number;
		public var MaxAttack:Number;
		public var MaxDefense:Number;
		public var MaxSpAtk:Number;
		public var MaxSpDef:Number;
		public var MaxSpeed:Number;
		public var MaxAccuracy:Number=95;
		public var MaxEvasive:Number=5;
		public var MaxTotal:Number;
		public var TemHP:Number;
		public var TemAttack:Number;
		public var TemDefense:Number;
		public var TemSpAtk:Number;
		public var TemSpDef:Number;
		public var TemSpeed:Number;
		public var TemAccuracy:Number;
		public var TemEvasive:Number;
		public var Move1:PokemonMove;
		public var Move2:PokemonMove;
		public var Move3:PokemonMove;
		public var Move4:PokemonMove;
		public var Exp:XMLList;
		public var Rate:Number=6;
		public var Status:String;
		public var StatusLost:Number=0;
		public var StatusTurn:Number=0;
		public var Flinching:Boolean=false;
		public var CurentMove:String="";
		public var AttackTurn:Number=0;
		public var AttackTurnBonus:Number=0;
		public var AttackFunction:String="";
		public var weatherAbi:Boolean=false;
		public var restoresHPEachTurnCount:Number=0;
		public var restoresHPEachTurnVaule:Number=0;		

		const BUG = 0;
		const DARK = 1;
		const DRAGON = 2;
		const ELECTRIC = 3;
		const FIGHTING = 4;
		const FIRE = 5;
		const FLYING = 6;
		const GHOST = 7;
		const GRASS = 8;
		const GROUND = 9;
		const ICE = 10;
		const NORMAL = 11;
		const POISON = 12;
		const PSYCHIC = 13;
		const ROCK = 14;
		const STEEL = 15;
		const WATER = 16;
		const NONE = 17;

		public function PokemonObject(Pokemon:XMLList,move1:XMLList,move2:XMLList,move3:XMLList,move4:XMLList) {
			upid=-1;
			pid=Pokemon.No;
			pmod=Pokemon.pmod;
			Name=Pokemon.name();
			Type1=Pokemon.Type1;
			Type2=Pokemon.Type2;
			Ability=Pokemon.Abi;
			MaxHP=Pokemon.HP;
			HP=MaxHP;
			OldHP=MaxHP;
			MaxAttack=Pokemon.Attack;
			MaxDefense=Pokemon.Defense;
			MaxSpAtk=Pokemon.SpAtk;
			MaxSpDef=Pokemon.SpDef;
			MaxSpeed=Pokemon.Speed;
			MaxTotal=Pokemon.Total;
			Exp=Pokemon.Exp;
			Move1=new PokemonMove(move1);
			Move2=new PokemonMove(move2);
			Move3=new PokemonMove(move3);
			Move4=new PokemonMove(move4);
			recoveryStatByMax();
			Status="";
			StatusLost=20;
		}
		
		public function GetMyPositionInList():Number{
			for(var i:int=0;i<SQLData.pokemonList.length;i++){
				if(SQLData.pokemonList[i].upid==upid) return i;
			}
			return -1;
		}
		
		public function recoveryStatByMax() {
			TemAttack=MaxAttack;
			TemDefense=MaxDefense;
			TemSpAtk=MaxSpAtk;
			TemSpDef=MaxSpDef;
			TemSpeed=MaxSpeed;
			TemEvasive=MaxEvasive;
			TemAccuracy=MaxAccuracy;
			setStatByTem();
		}
		public function recoveryBeforeBattle(){
			recoveryStatByMax();
			checkAbility();
			Flinching=false;
			CurentMove="";
			AttackTurn=0;
			AttackTurnBonus=0;
			AttackFunction="";
			weatherAbi=false;
		}
		public function setStatByTem() {
			Attack=TemAttack;
			Defense=TemDefense;
			SpAtk=TemSpAtk;
			SpDef=TemSpDef;
			Speed=TemSpeed;
			Evasive=TemEvasive;
			Accuracy=TemAccuracy;
		}
		public function addBonus(_HP:Number,_Attack:Number,_Defense:Number,_SpAtk:Number,_SpDef:Number,_Speed:Number) {
			MaxHP+=Number(_HP);
			MaxAttack+=Number(_Attack);
			MaxDefense+=Number(_Defense);
			MaxSpAtk+=Number(_SpAtk);
			MaxSpDef+=Number(_SpDef);
			MaxSpeed+=Number(_Speed);
			HP=MaxHP;
			recoveryStatByMax();
			recoveryTotal();
		}
		public function getEXP(_HP:Number,_Attack:Number,_Defense:Number,_SpAtk:Number,_SpDef:Number,_Speed:Number) {
			MaxHP+=Number(_HP);
			MaxAttack+=Number(_Attack);
			MaxDefense+=Number(_Defense);
			MaxSpAtk+=Number(_SpAtk);
			MaxSpDef+=Number(_SpDef);
			MaxSpeed+=Number(_Speed);
			recoveryStatByMax();
		}
		public function setStat(_HP:Number,_Attack:Number,_Defense:Number,_SpAtk:Number,_SpDef:Number,_Speed:Number,_Total:Number) {
			MaxHP=Number(_HP);
			MaxAttack=Number(_Attack);
			MaxDefense=Number(_Defense);
			MaxSpAtk=Number(_SpAtk);
			MaxSpDef=Number(_SpDef);
			MaxSpeed=Number(_Speed);
			recoveryStatByMax();
			Total=_Total;
		}
		public function setNewTemStat(sName:String, sValue:Number) {
			this["Tem"+sName]=Math.round(sValue);
			this[sName]=this["Tem"+sName];
		}
		public function recoveryTotal() {
			MaxTotal=MaxHP+MaxAttack+MaxDefense+MaxSpAtk+MaxSpDef+MaxSpeed;
			Total=MaxTotal;
		}
		public function recoveryAll() {
			HP=MaxHP;
			recoveryStatByMax();
			recoveryTotal();
			removeStatus();
		}
		public function restoresHPEachTurn(){
			if(restoresHPEachTurnCount>0){
				HP+=restoresHPEachTurnVaule;
				setMaxHP();
				restoresHPEachTurnCount--;
			}				
		}
		public function moveChangeStat(nStat:String,eStat:Number):Number {
			var realValue:Number= eStat;
			if (Ability=="ClearBody" && realValue<0) {
				trace(Name+" "+Ability);
				return 0;
			}
			if (Ability=="BigPecks" && realValue<0 && nStat=="Defense") {
				trace(Name+" "+Ability);
				return 0;
			}
			if (Ability=="HyperCutter" && realValue<0 && nStat=="Attack") {
				trace(Name+" "+Ability);
				return 0;
			}
			if (Ability=="KeenEye" && realValue<0 && nStat=="Accuracy") {
				trace(Name+" "+Ability);
				return 0;
			}
			if (Math.abs(realValue)<1) {
				realValue=Math.round(realValue*this["Max"+nStat]);
			}
			if(nStat=="HP"){
				HP+=realValue;
				setMaxHP();				
			}else{
				this["Tem"+nStat]+=realValue;
				this[nStat]=this["Tem"+nStat];
			}
			return realValue;
		}
		public function checkAbility() {
			recoveryMove();
			switch (Ability) {
				case "Adaptability" :
					addBonusMoveAtkByAbility(Type1,1.2);
					addBonusMoveAtkByAbility(Type2,1.2);
					break;
				case "Compoundeyes" :
					Accuracy=100;
					break;
				case "HugePower" :
					setNewTemStat("Attack",MaxAttack*2);
					break;
				case "Hustle" :
					setNewTemStat("Attack",MaxAttack*1.5);
					setMoveAccByCategory("Physical",0.8);
					break;
			}
		}
		public function recoveryMove() {
			for (var i:Number=1; i<5; i++) {
				this["Move"+i].setDefaultValue();
			}
		}
		public function addBonusMoveAtkByAbility(mType:String, mBoot:Number) {
			for (var i:Number=1; i<5; i++) {
				if (this["Move"+i].Type==mType) {
					this["Move"+i].Attack=Math.round(this["Move"+i].MaxAttack*mBoot);
				}
			}
		}
		public function setMoveAccByCategory(mCate:String, mRec:Number) {
			for (var i:Number=1; i<5; i++) {
				if (this["Move"+i].Category==mCate) {
					this["Move"+i].Accuracy=Math.round(this["Move"+i].MaxAccuracy*mRec);
				}
			}
		}
		public function set0HP() {
			if (HP<0) {
				HP=0;
			}
		}
		public function setMaxHP() {
			if (HP>MaxHP) {
				HP=MaxHP;
			}
		}
		public function isStatusEffect():Boolean {
			var returnDamage:Number=0;
			var rndNumber:Number;
			if (Status!="") {
				trace(Name+" "+Status);
				switch (Status) {
					case "Sleep" :
						if (StatusTurn==0) {
							removeStatus();
							return false;
						} else {
							StatusTurn--;
						}
						break;
					case "Frozen" :

						rndNumber=Math.round(Math.random()*9);
						if (rndNumber==1) {
							removeStatus();
							return false;
						}
						break;
					case "Poison" :

						HP-=StatusLost;
						set0HP();
						break;
					case "Burn" ://Confuse
						HP-=StatusLost;
						set0HP();
						break;
					case "Confuse" :
						rndNumber=Math.round(Math.random()*1);
						if (rndNumber==1) {
							var selfAttack:Number=Math.round(MaxHP/7);
							HP-=selfAttack;
							set0HP();
							trace(Name+" " +Status+" -"+selfAttack);
							return true;
						} else {
							var conRandom:Number=Math.round(Math.random()*1);
							if (conRandom==1) {
								trace(Name+" End Confuse");
								removeStatus();
							}
							return false;
						}
						break;
					case "Parlyz" :
						rndNumber=Math.round(Math.random()*4);
						if (rndNumber>1) {
							return true;
						} else {
							var pazRandom:Number=Math.round(Math.random()*1);
							if (pazRandom==1) {
								trace(Name+" End Parlyz");
								removeStatus();
							}
							return false;
						}
						break;
				}
				return true;
			} else {
				setStatByTem();
				return false;
			}
		}
			
		public function setStatusBeforeBattle() {
			switch (Status) {
					case "Burn" :
						StatusLost=Math.round(MaxHP/8);
						if (Ability!="Guts" && Ability!="FlareBoost") {
							SpAtk=Math.round(TemSpAtk/2);
						}
						if (Ability=="FlareBoost") {
							SpAtk=TemSpAtk*1.5;
						}
						break;
					case "Poison" :
						StatusLost=Math.round(MaxHP/8);
						break;
					case "Sleep" :
						StatusTurn=Math.round(Math.random()*6)+1;
						if (Ability=="EarlyBird") {
							StatusTurn=Math.round(StatusTurn/2);
						}
						break;
					case "Parlyz" :
						Status="";
						break;
					case "Confuse" :
						Status="";
						break;
			}
			if (Ability=="Guts" && (Status=="Burn" ||Status=="Parlyz" ||Status=="Poison" ||Status=="Sleep")) {
				Attack=TemAttack*2;
			}
		}
		public function setStatus(_Status:String) {
			if (Status=="") {
				switch (_Status) {
					case "Burn" :
						if (Type1=="FIRE"||Type2=="FIRE" ||Ability=="Heatproof") {
							_Status=="";
						} else {
							StatusLost=Math.round(MaxHP/8);
							if (Ability!="Guts") {
								SpAtk=Math.round(TemSpAtk/2);
							}//setStatByTemus("SpAtk",Math.round(TemSpAtk/2));
						}
						if (Ability=="FlareBoost") {
							SpAtk=TemSpAtk*1.5;
						}
						break;
					case "Parlyz" :
						//StatusLost=Math.round(MaxHP/8);
						Speed-=Math.round(MaxSpeed/4);
						break;
					case "Poison" :
						if (Type1=="STEEL"||Type2=="STEEL" || Type1=="POISON"||Type2=="POISON" || Ability=="Immunity") {
							_Status=="";
						} else {
							StatusLost=Math.round(MaxHP/8);
						}
						break;
					case "Sleep" :
						StatusTurn=Math.round(Math.random()*6)+1;
						if (Ability=="EarlyBird") {
							StatusTurn=Math.round(StatusTurn/2);
						}
						break;
					case "Frozen" :
						if (Type1=="ICE"||Type2=="ICE") {
							_Status=="";
						}
						break;
					case "Confuse" :
						if (Ability=="OwnTempo") {
							_Status=="";
						}
						break;

				}
				Status=_Status;
				if (Ability=="Guts" && (Status=="Burn" ||Status=="Parlyz" ||Status=="Poison" ||Status=="Sleep")) {
					Attack=TemAttack*2;
				}
			}
			//StatusLost=mEffect;
		}
		public function useItem(_item:ItemObject) {
			moveChangeStat(_item.Stat,_item.Effect)
			if (HP>MaxHP) {
				HP=MaxHP;
			}
			_item.NumberOf--;
			SQLData.UpdatePokemonSQL(this);
			SQLData.UpdateItemSQL(_item,-1);
		}
		public function removeStatus() {
			Status="";
			StatusLost=0;
			StatusTurn=0;
		}
		public function calRate() {
			if (Total<=300) {
				Rate=4;
			} else if (Total<=400) {
				Rate=6;
			} else if (Total<=500) {
				Rate=8;
			} else if (Total<=600) {
				Rate=10;
			} else {
				Rate=12;
			}
		}
		public function setRate(newRate:Number) {
			if (newRate<1) {
				newRate=1;
			}
			Rate=newRate;
		}
		public function addRate(newRate:Number) {
			Rate+=newRate;
			if (Rate<1) {
				Rate=1;
			}
		}
		public function setLegend() {
			Rate=150;
		}
	}
}